/*
 * @Date: 2025-09-02 22:49:27
 * @LastEditors: 贾二小 erxiao.jia@outlook.com
 * @LastEditTime: 2025-10-27 21:41:20
 * @FilePath: /admin-vue/src/composables/system/usePermission.ts
 */
import { http } from '@/plugins/axios'

export default () => {
  const loading = ref(false)
  const collection = ref<ResponsePageResult<PermissionModel>>()
  const params = ref({
    key: '',
    page: 1,
    per_page: 5,
  })
  const model = ref<PermissionModel>()

  const load = async () => {
    loading.value = true
    collection.value = await http.request<PermissionModel[], ResponsePageResult<PermissionModel>>({
      url:
        `permission?` +
        Object.entries(params.value)
          .map(([k, v]) => `${k}=${v}`)
          .join('&'),
    })
    loading.value = false
  }

  const find = async (id: any) => {
    const { data } = await http.request<PermissionModel>({
      url: `permission/${id}`,
    })
    return data
  }

  const add = async (model: PermissionModel) => {
    const data = await http.request({ url: 'permission', method: 'POST', data: model })
    return data
  }

  const update = async (model: PermissionModel) => {
    const data = await http.request<any, ResponseResult<any>>({
      url: `permission/${model.id}`,
      method: 'PUT',
      data: model,
    })
    return data
  }

  const del = async (id: any) => {
    await http.request({ url: `permission/${id}`, method: 'DELETE' })
    load()
  }

  const permissions = async () => {
    const { data } = await http.request<TransferOptionType[]>({
      url: `permission/all`,
    })
    return data
  }

  return { loading, collection, params, model, load, find, add, update, del, permissions }
}
